<?php
require_once(mcz_LIB_DIR . 'getid3/getid3.php');
class mcz_AudioTags
{
    private static $getid3;
    //private static $getid3Writer;

    public static function read($file)
    {
        try
        {
            /*
             Being php4 compatible, getid3 throws all kinds of E_STRICT so it's
             better to turn errors off.
            */
            $infos = @self::getid3()->Analyze($file);
            @getid3_lib::CopyTagsToComments($infos);
        }
        catch(Exception $e)
        {
            return false;
        }
        return array(
                     'artist'  => @trim($infos['comments']['artist'][0]),
                     'title'   => @trim($infos['comments']['title'][0]),
                     'track'   => @trim($infos['comments']['track'][0]
                                    ? $infos['comments']['track'][0]
                                    : @$infos['comments']['tracknumber'][0]),
                     'album'   => @trim($infos['comments']['album'][0]),
                     'year'    => @trim($infos['comments']['year'][0]
                                    ? $infos['comments']['year'][0]
                                    : @$infos['comments']['date'][0]),
                     'genre'   => @trim($infos['comments']['genre'][0]),
                     'comment' => @trim($infos['comments']['comment'][0]),
                     'time'    => @trim($infos['playtime_seconds'] ? $infos['playtime_seconds'] : 0)
                     );
    }

    private static function getid3()
    {
        if(empty(self::$getid3))
        {
            self::$getid3 = new getID3();
            self::$getid3->encoding = 'UTF-8';
        }
        return self::$getid3;
    }

    /*
    public static function write($file, array $tags)
    {
        $tagData = array();
        self::getid3Writer()->filename = $file;
        foreach($tags as $key => $val)
        {
            $tagData[$key][] = $val;
        }
        self::getid3Writer()->tag_data = $tagData;
        self::getid3Writer()->WriteTags();
    }
    */

    /*
    private static function getid3Writer()
    {
        if(empty(self::$getid3Writer))
        {
            self::$getid3Writer = new getid3_writetags();
            self::$getid3Writer->tagformats = array('id3v1', 'id3v2.3');
            self::$getid3Writer->overwrite_tags = false;
            self::$getid3Writer->tag_encoding   = 'UTF-8';
            self::$getid3Writer->remove_other_tags = false;
        }
        return self::$getid3Writer;
    }
    */

}